/*Hdip Project 2013-14, Student Name:- Chris Pembroke,  Student Number:- x00115839*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Exercise1Project.Models;                  // added namespace references
using Exercise1Project.Models.Validation;
using System.Data.Entity.Validation;
using System.Diagnostics;

namespace Exercise1Project.Controllers
{
    public class CustomerDetailsController : Controller
    {
        private BicycleRepairEntities2 _db = new BicycleRepairEntities2();

        //private ICustomerDetailRepository _repository;  // instance of business logic  interface

        private ICustomerDetailBusinessLogic _service;

        public CustomerDetailsController()
        {
            _service = new CustomerDetailBusinessLogic(new ModelStateWrapper(this.ModelState));
        }

        public CustomerDetailsController(ICustomerDetailBusinessLogic service)  // parameterised constructor used for test purposes
        {
            _service = service;
        }
        //
        // GET: /CustomerDetails/
        public ActionResult CustomerDetail(string id)  // get the details of customer (user in the database)
        {
           var customerDetails = _service.GetCustomerDetailsByCustomerId(id);
            if(customerDetails == null)
            {
                return HttpNotFound();
            }
            return View("CustomerDetail", customerDetails);
        }

        //GET .../Edit or update customer details
        [HttpGet]
        public ActionResult Edit(string id)
        {
            User customerToUpdate = _service.GetCustomerDetailsByCustomerId(id);
            if(customerToUpdate == null)
            {
                return HttpNotFound();
            }
            return View("Edit", customerToUpdate);
        }

        //POST .../Edit or update customer details
        [HttpPost]
        public ActionResult Edit(User userToUpdate)
        {
              
            if(ModelState.IsValid)
            {
                if(_service.UpdateCustomerDetails(userToUpdate))
                {
                     return RedirectToAction("CustomerDetail", new { id = userToUpdate.Id });
                }
            }
            return View("Edit", userToUpdate);         
        }

        public ActionResult RedirectToBikes(string id)
        {
            return RedirectToAction("Index", "Bike", new { id = id });
        }

	}
}
